package com.lm.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lm.mapper.DocterAndTeamMapper;
import com.lm.mapper.DocterMapper;
import com.lm.mapper.OrgMapper;
import com.lm.mapper.TeamManageMapper;
import com.lm.pojo.Docter;
import com.lm.pojo.DocterAndTeam;
import com.lm.pojo.TeamManage;
import com.lm.service.TeamManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class TeamManageServicempl implements TeamManageService {

    @Autowired
    DocterAndTeamMapper docterAndTeamMapper;
    @Autowired
    private TeamManageMapper teamManageMapper;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private DocterMapper docterMapper;

    // 实现“查询我管理的团队”
    @Override
    public List<TeamManage> findMyTeam(Integer doctorId) {
        // 调用Mapper查询，返回真实团队数据
        return teamManageMapper.findMyTeam(doctorId);
    }

    @Override
    public List<TeamManage>findMyJoinTeam(Integer docterId){
        //team Id by docterId
        QueryWrapper<DocterAndTeam> queryWrapper = new QueryWrapper<DocterAndTeam>();
        queryWrapper.eq("docter_id",docterId);
        List<DocterAndTeam> teamId = docterAndTeamMapper.selectList(queryWrapper);
        List<TeamManage> teamManages = new ArrayList<>();

        for(DocterAndTeam team:teamId){
            TeamManage teamManage = teamManageMapper.selectById(team.getDocterTeamId());
            Docter docter = docterMapper.selectById(team.getDocterId());
            teamManage.setOrgName(orgMapper.selectById(teamManage.getDocterTeamOrgId()).getOrgName());
            teamManage.setLeaderName(docter.getDocterName());
            teamManages.add(teamManage) ;
        }
        return teamManages;

    }
}